package Netty;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;

/**
 * Created by brossdong on 2017/12/11.
 */
public class Client {

    public static void main(String[] args)throws Exception {

        EventLoopGroup group = new NioEventLoopGroup();
        Bootstrap b = new Bootstrap();
        b.group(group)
         .channel(NioSocketChannel.class)
         .handler(new ChannelInitializer<SocketChannel>() {
         @Override
         protected void initChannel(SocketChannel sc) throws Exception {
             //设置特殊分隔符
             ByteBuf byteBuf = Unpooled.copiedBuffer("$_".getBytes());
             sc.pipeline().addLast(new DelimiterBasedFrameDecoder(1024,byteBuf));
             //设置字符串形式解码
             sc.pipeline().addLast(new StringDecoder());
             sc.pipeline().addLast(new ClientHandler());
             }
         });

        //多端口
        ChannelFuture cf1 = b.connect("127.0.0.1", 8765).sync();
        //ChannelFuture cf2 = b.connect("127.0.0.1", 8764).sync();
        //发送消息
        //Thread.sleep(1000);
        cf1.channel().writeAndFlush(Unpooled.copiedBuffer("aaa$_".getBytes()));
        cf1.channel().writeAndFlush(Unpooled.copiedBuffer("bbbb$_".getBytes()));
        cf1.channel().writeAndFlush(Unpooled.copiedBuffer("ccccc$_".getBytes()));
        //cf1.channel().writeAndFlush(Unpooled.copiedBuffer("666".getBytes()));
        //cf2.channel().writeAndFlush(Unpooled.copiedBuffer("888".getBytes()));
        //Thread.sleep(2000);
        //cf1.channel().writeAndFlush(Unpooled.copiedBuffer("888".getBytes()));
        //cf2.channel().writeAndFlush(Unpooled.copiedBuffer("666".getBytes()));

        cf1.channel().closeFuture().sync();
        //cf2.channel().closeFuture().sync();
        group.shutdownGracefully();
    }

}
